package com.be.beadmin.system.controller.ProductQualityController;

import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.product.dto.WorkOrderProductPlanDto;
import com.be.beadmin.product.dto.input.ScheduleDto;
import com.be.beadmin.product.entity.ProductManufactInfor;
import com.be.beadmin.product.repository.ProductManufactInforRepository;
import com.be.beadmin.system.service.ProductQualityService.ProductManufactInforPqService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description:
 * @author: Jiaming Zheng
 * @Date: Create in 15:07 2023/6/20
 * @Modified By:
 **/
@Api(tags = {"生产-质量公共模块-生产计划执行"})
@RestController
public class ProductManufactInforPqController {

    @Autowired
    private ProductManufactInforPqService productManufactInforPqService;
    @Autowired
    private ProductManufactInforRepository productManufactInforRepository;

    @ApiOperation("开始执行工序")  // 开始执行 需要改成每个人单独的工时
    @PostMapping(value = "/product/startProductManufactExec")
    public ResultModel startProductManufactExec(HttpServletRequest request, @ApiParam(value = "工序执行的id") @RequestParam String productManufactInforId) {

//        try {
            int re = productManufactInforPqService.startProductManufactExec(request,productManufactInforId);
            if (re == 0) {
                return ResultModel.success("工序开始执行");
            }
            if (re == -1) {
                return ResultModel.error(re, "工序已经开始无法再次开始");
            }
            return ResultModel.success();
//        } catch (Exception e) {
//            return ResultModel.error(-1, e.getMessage());
//        }
    }

    @ApiOperation("完成执行工序") // 完成执行 需要改成每个人单独的工时
    @PostMapping(value = "/product/finishProductManufactExec")
    public ResultModel finishProductManufactExec(HttpServletRequest request, @ApiParam(value = "工序执行的id") @RequestParam String productManufactInforId) {

        ProductManufactInfor productManufactInfor = productManufactInforRepository.findProductManufactInforByIdAndDelete(productManufactInforId, 0);
//        try {
            int re = productManufactInforPqService.finishProductManufactExec(request,productManufactInforId);
            if (re == 0) {
                return ResultModel.success("工序完成执行");
            }
            if (re == -1) ResultModel.error(re, "工序已经完成无法再次完成");
            return ResultModel.success();
//        } catch (Exception e) {
//            return ResultModel.error(-1, e.getMessage());
//        }
    }


    @ApiOperation("手动排程")
    @PostMapping("/product/manualSchedule")
    public ResultModel manualSchedule(@RequestBody WorkOrderProductPlanDto workOrderProductPlanDto) {
        productManufactInforPqService.manualSchedule(workOrderProductPlanDto);
        return ResultModel.success(ResultModel.UPDATE);
    }

    @ApiOperation("批量排程")
    @PostMapping("/product/scheduleByWoIds")
    public ResultModel scheduleByWoIds(@RequestBody ScheduleDto scheduleDto) {
        productManufactInforPqService.scheduleByWoIds(scheduleDto, true);
        return ResultModel.success(ResultModel.UPDATE);
    }
}
